package com.googlecode.antilopebpm.variable.instance.reference;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.googlecode.antilopebpm.cfg.AntilopeConfiguration;
import com.googlecode.antilopebpm.util.ByteBlockChopper;

public class ByteArray {

	private static final long serialVersionUID = 1L;

	long id = 0;
	protected List<byte[]> byteBlocks = null;

	public ByteArray() {
	}

	public ByteArray(byte[] bytes) {
		this.byteBlocks = ByteBlockChopper.chopItUp(bytes,
				AntilopeConfiguration.VARIABLE_BYTE_BLOCK_SIZE);
	}

	public ByteArray(ByteArray other) {
		List<byte[]> otherByteBlocks = other.getByteBlocks();
		if (otherByteBlocks != null) {
			this.byteBlocks = new ArrayList<byte[]>(otherByteBlocks);
		}
	}

	public byte[] getBytes() {
		return ByteBlockChopper.glueChopsBackTogether(byteBlocks);
	}

	public long getId() {
		return id;
	}

	public boolean equals(Object o) {
		if (o == null)
			return false;
		if (!(o instanceof ByteArray))
			return false;
		ByteArray other = (ByteArray) o;
		return Arrays.equals(
				ByteBlockChopper.glueChopsBackTogether(byteBlocks),
				ByteBlockChopper.glueChopsBackTogether(other.byteBlocks));
	}

	public int hashCode() {
		if (byteBlocks == null)
			return 0;
		return byteBlocks.hashCode();
	}

	public List<byte[]> getByteBlocks() {
		return byteBlocks;
	}
}
